package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.tour.pojo.User;
import com.bdqn.tour.service.HzyInfoVerifyService;
import com.bdqn.tour.service.impl.HzyInfoVerifyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updatePhone")
public class UpdatePhoneServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/plain;charset=UTF-8");

        String phone = req.getParameter("phone");

        User user = (User) req.getSession().getAttribute("user");
        int id = user.getId();

        User update = new User();

        update.setIpone(phone);
        update.setId(id);

        HzyInfoVerifyService service = new HzyInfoVerifyServiceImpl();
        JSONObject result = new JSONObject();
        if (service.updateUser(update)) {
            result.put("code", "0000");
            result.put("message", "修改成功");
            // 更新session中的用户信息
            user.setIpone(phone);
            req.getSession().setAttribute("user", user);
        } else {
            result.put("code", "0001");
            result.put("message", "修改失败");
        }
        resp.getWriter().write(result.toString());
    }
}
